/**
 * Vuex管理数据
 */
//引入vue
import Vue from 'vue'
//引入vuex
import Vuex from 'vuex'

import { getUser,updateUser } from '@/api/user'

Vue.use(Vuex)
//用于相应组件的动作
const actions = {
    getUser(context,params){ 
        getUser(params).then(res => { 
            context.commit('getUser',res.data)
        })
    },
    UpdateAvatar(context,avatar){ 
        context.commit('UpdateAvatar',avatar)
    },
    updateUser(context,user){ 
        context.commit('updateUser',user)
    }
} 
//操作数据
const mutations = {
    getUser(context,User){ 
        state.User = User
    },
    UpdateAvatar(context,avatar){ 
        state.User.avatar = avatar
    },
    updateUser(context,user){ 
        state.User = {...user}
    }
} 

let result = JSON.parse(window.localStorage.getItem("result"))
let user = {}

//防止刷新页面后vuex数据丢失
if(result != null){ 
    user = result.user
}


//状态管理
const state ={
    User:user
}

export default new Vuex.Store({
    actions,
    mutations,
    state
})
